home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #1 / Ham Radio 2000.iso / ham2000 / tcp_ip / tnos / tnos100s / sw.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-30  |  2.4 KB  |  93 lines

  1. /* These routines, plus the assembler hooks in stopwatch.asm, implement a
  2.  * general purpose "stopwatch" facility useful for timing the execution of
  3.  * code segments. The PC's "timer 2" channel (the one ordinarily
  4.  * used to drive the speaker) is used. It is driven with a 838 ns
  5.  * clock. The timer is 16 bits wide, so it "wraps around" in only 55 ms.
  6.  *
  7.  * There is an array of "stopwatch" structures used for recording the number
  8.  * of uses and the min/max times for each. Since only one hardware timer is
  9.  * available, only one stopwatch can actually be running at any one time.
  10.  *
  11.  * This facility is useful mainly for timing routines that must execute with
  12.  * interrupts disabled. An interrupt that occurs while the timer is running
  13.  * will not stop it, so it would show an errneously large value.
  14.  *
  15.  * To start a timer, call swstart(). To stop it and record its value,
  16.  * call swstop(n), where n is the number of the stopwatch structure.
  17.  * The stopwatch structures can be displayed with the "watch" command.
  18.  *
  19.  * Copyright 1991 Phil Karn, KA9Q
  20.  */
  21. /* Mods by PA0GRI */
  22.  
  23. #include "global.h"
  24. #include "config.h"
  25. #include "cmdparse.h"
  26. #include "pc.h"
  27.  
  28. #ifdef ALLCMD
  29. #ifdef SWATCH
  30. struct stopwatch Sw[NSW];
  31. char Swflag = 1;                        /* default is allow stop watch functions */
  32.  
  33. /* Stop a stopwatch and record its value.
  34.  * Uses stopval() routine in stopwatch.asm
  35.  */
  36. void
  37. swstop(n)
  38. int n;
  39. {
  40.     register int16 cnt;
  41.     register struct stopwatch *sw;
  42.  
  43.     if((int)!Swflag)
  44.         return;                 /* no stop watch functions */
  45.  
  46.     cnt = stopval();
  47.     sw = &Sw[n];
  48.  
  49.     if(sw->calls++ == 0){
  50.         sw->maxval = cnt;
  51.         sw->minval = cnt;
  52.     } else if(cnt > sw->maxval){
  53.         sw->maxval = cnt;
  54.     } else if(cnt < sw->minval){
  55.         sw->minval = cnt;
  56.     }
  57. }
  58. int
  59. doswatch(argc,argv,p)
  60. int argc;
  61. char *argv[];
  62. void *p;
  63. {
  64.     register struct stopwatch *sw;
  65.     long maxval,minval;
  66.     int i, ret, flag;
  67.  
  68.     if(argc > 1){
  69.         /* Clear timers */
  70.         for(i=0,sw=Sw;i < NSW;i++,sw++){
  71.             sw->calls = 0;
  72.         }
  73.     }
  74.     flag = (int)Swflag;
  75.     ret = setbool(&flag,"Stop Watch flag",argc,argv);
  76.     Swflag = (char)flag;
  77.     
  78.     if(ret != 0 || argc > 1)
  79.         return ret;
  80.  
  81.     for(i=0,sw=Sw;sw < &Sw[NSW];i++,sw++){
  82.         if(sw->calls != 0){
  83.             minval = (65536 - sw->maxval) * 838 / 1000;
  84.             maxval = (65536 - sw->minval) * 838 / 1000;
  85.             tprintf("%d: calls %ld min %ld max %ld\n",
  86.              i,sw->calls,minval,maxval);
  87.         }
  88.     }
  89.     return 0;
  90. }
  91. #endif /*SWATCH*/
  92. #endif /*ALLCMD*/
  93.